package com.szzg10.mina2.chapter9;

import java.net.InetSocketAddress;

import org.apache.mina.core.future.ConnectFuture;
import org.apache.mina.core.session.IoSession;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketConnector;

public class ImageClient {

	public static final String HOSTNAME = "127.0.0.1";
	public static final int PORT = 33789;
	
	public void start() {
		
		NioSocketConnector connector = new NioSocketConnector();
		
		connector.getFilterChain().addLast("logger", new LoggingFilter());
		connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new ImageCodecFactory(true)));
		
		connector.setHandler(new ImageClientHandler());
		
		ConnectFuture future = connector.connect(new InetSocketAddress(HOSTNAME, PORT));
		future.awaitUninterruptibly();
		
		IoSession session = future.getSession();
		
		ImageRequest request = new ImageRequest(100, 200, 300);
		session.write(request);
	}
}
